import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Hello extends AuthorizingRealm {
    private static final String[] hexDigits = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
    public static void main(String[] args) {
        String salt = "f66074e04a8196fbba1527ac36da2543";
        System.out.println(encodeByMD5("123456"));
        //UsernamePasswordToken token = new UsernamePasswordToken("admin"+"f66074e04a8196fbba1527ac36da2543", encodeByMD5("123456"));
        UsernamePasswordToken token = new UsernamePasswordToken("admin", encodeByMD5("654321").toUpperCase());
        System.out.println(new String(token.getUsername()));
        System.out.println(new String(token.getUsername()));
        System.out.println(new String(token.getPassword()));
        System.out.println(new String(token.getPassword()));
        System.out.println("密码" + (new Md5Hash(token.getPassword(),token.getUsername()+salt, 2)).toBase64());

        System.out.println(new SecureRandomNumberGenerator().nextBytes().toHex());
        /*Subject currentUser = SecurityUtils.getSubject();
        currentUser.login(token);
        //UsernamePasswordToken token = (UsernamePasswordToken)authcToken;
        String username = new String(token.getUsername());
        String password = new String(token.getPassword());*/
        System.out.println("oycVT54wIQeaw/Z833JagA==".equals("oycVT54wIQeaw/Z833JagA=="));

        //测试javalist
        List<Long> list = new ArrayList<>();
        list.add((long)1);
        list.add((long)2);
        list.add((long)3);
        list.addAll(new ArrayList<Long>(Arrays.asList((long)4,(long)5,(long)6)));

        list.forEach(v->{
            System.out.printf("值：%s",v);
            System.out.printf("list-size：%s",list.size());
        });

        String name = "abcdefghijk";
        System.out.println(name.substring(7));
        //

    }

    private static String encodeByMD5(String originString) {
        if (originString != null) {
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                byte[] results = md.digest(originString.getBytes());
                String resultString = byteArrayToHexString(results);
                return resultString.toUpperCase();
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

        return null;
    }

    private static String byteArrayToHexString(byte[] b) {
        StringBuffer resultSb = new StringBuffer();

        for(int i = 0; i < b.length; ++i) {
            resultSb.append(byteToHexString(b[i]));
        }

        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (b < 0) {
            n = b + 256;
        }

        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    public void ShiroRealm()
    {
    }

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
            throws AuthenticationException
    {
        UsernamePasswordToken token = (UsernamePasswordToken)authcToken;
        String username = new String(token.getUsername());
        String password = new String(token.getPassword());
        System.out.println("账号："+username);
        System.out.println("密码："+password);
        return null;
    }



    private void setSession(Object key, Object value)
    {
        Subject currentUser = SecurityUtils.getSubject();
        if(currentUser != null)
        {
            Session session = currentUser.getSession();
            if(session != null)
                session.setAttribute(key, value);
        }
    }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
}
